import {message} from 'antd';
import {formatMessage} from './intl';

export const newOrUpdateOrderValidator = (data, details) => {
    const requiredFields = [];
    if(!data || !data.dealerId)
        requiredFields.push(
            formatMessage({
                id: 'validator.dealerId',
                defaultMessage: '总代'
            })
        );
    if(!data || !data.brandId)
        requiredFields.push(
            formatMessage({
                id: 'validator.brandId',
                defaultMessage: '品牌'
            })
        );
    if(requiredFields.length > 0) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.requied',
            defaultMessage: '必填'
        })}`);
        return false;
    }
    if(!details || (details && details.length === 0)) {
        message.warning(formatMessage({
            id: 'validator.message.detailQuantity',
            defaultMessage: '清单不能为空！'
        }));
        return false;
    }
    if(details.find(d => d.assembleMethod === undefined || d.assembleMethod === 0)) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.assembleMethod',
            defaultMessage: '清单中装配方式必填!'
        })}`);
        return false;
    }
    if(details.find(d => !d.materialGroupKDBOMCode && !d.referenceMaterialGroupKDBOMCode)) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.materialGroupKDBOMCode',
            defaultMessage: '清单中物料组号和参考物料组号必填一项!'
        })}`);
        return false;
    }
    if(details.find(d => d.versionCode === undefined || d.versionCode === '')) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.versionCode',
            defaultMessage: '清单中版型编号必填!'
        })}`);
        return false;
    }
    if(details.find(d => d.newModel !== 1 && d.newModel !== 0 && d.newModel !== true && d.newModel !== false)) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.newModel',
            defaultMessage: '清单中是否新车型必填!'
        })}`);
        return false;
    }
    return true;
};

